package com.dot.dominion.view;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.dot.dominion.Constants;
import com.dot.dominion.Game;
import com.dot.dominion.R;
import com.dot.dominion.domain.Card;
import com.dot.dominion.domain.Hand;

//Darren-- Needs to be udpated after God Class
public class HandView extends ListActivity implements OnClickListener {
	private Hand hand;
	private int playerNumber;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.handview );
        
        Intent parameters = getIntent();
        if( parameters.hasExtra(Constants.PLAYER_NUMBER) ) {
        	playerNumber = parameters.getIntExtra(Constants.PLAYER_NUMBER,
        			Constants.GENERAL_ERROR);
        }
        else {
        	//without player number, we have no hand to refer to
        	Log.d("DEBUGTAG", "Hand not initialized!");
        }
        
        //Get player's hand
        Game _game = (Game) getApplication();
        hand = _game.getHand(playerNumber);
        _game = null;
        
        //Set List for displaying the Hand
        setListAdapter( new HandAdapter(this, hand.toArray()) );
        
        //Discard card button
        View returnButton = findViewById( R.id.return_button );
        returnButton.setOnClickListener( this );
    }
	
	@Override
	public void onListItemClick(ListView parent, View v, int position,
			long id) {
		Intent cardView = new Intent(this, CardView.class);
		
		Card card = hand.peekCard(position);
		
		cardView.putExtra(Constants.PICTURE_ID, card.getPictureId());
		
		startActivityForResult(cardView, Constants.CARD_VIEW);
	}
	
	public void onClick( View view ) {
		switch( view.getId() ) {
			case R.id.return_button:
				setResult(Activity.RESULT_CANCELED);
				finish();
				break;
		}
	}
}
